//
//  DiseaseInfo_viewController.m
//  International_Hospital
//
//  Created by 李建强 on 16/7/6.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "DiseaseInfo_viewController.h"
#import "DiseaserTopView.h"
#import "DesCell.h"
#import "DesModel.h"


@interface DiseaseInfo_viewController ()<UITableViewDelegate,UITableViewDataSource>



@property (nonatomic,strong)UITableView *tableview;

@property (nonatomic,strong)UIImageView *picImageView;

@property (nonatomic,strong)IH_Label *nameLabel;

@property (nonatomic,strong)IH_Label *profLabel;

@property (nonatomic,strong)IH_Label *hospitalLabel;


@property (nonatomic,strong)DiseaserTopView *topView;

@property (nonatomic,strong)NSArray<DesModel*> *dataArray;

@property (nonatomic,strong)UIBarButtonItem*rightItem;

@property (nonatomic,assign)BOOL EditOrOff;

@property (nonatomic,strong)DesCell*cell;

@end

@implementation DiseaseInfo_viewController

-(NSArray<DesModel *> *)dataArray{
    
    if (_dataArray == nil) {
        
        DesModel*model = [[DesModel alloc]init];
        _dataArray = [model desDataArray];
        
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置顶部右侧编辑按钮
    [self setHeadItem];
    [self setupUI];
    [self setupLayout];
}



-(void)setHeadItem{
    
    NSString*title;
    if (self.EditOrOff) {
        title = @"确定";
    }else{
        
        title = @"编辑";
    }
    
    self.rightItem = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(didClickEdit)];
    self.rightItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = self.rightItem;
    
}


-(void)didClickEdit{
    

    self.rightItem.title = @"确定";
    NSIndexPath *indexPath;
    DesCell *cell;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    
    NSMutableDictionary *muDict = [NSMutableDictionary dictionary];
    
    if (self.EditOrOff) {
        for (int i = 0; i< self.dataArray.count; i++) {
            indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            cell = [self.tableview cellForRowAtIndexPath:indexPath];
            
            if (i == 1) {
                
                if (self.disBlock) {
                    self.disBlock(cell.tx_Label.text);
                }
            }
            [muDict setObject:cell.tx_Label.text forKey:cell.titleLabel.text];
            [defaults setObject:cell.tx_Label.text forKey:cell.titleLabel.text];
            [defaults synchronize];
        }
        
        if (self.dicBlock) {
            self.dicBlock(muDict.copy);
        }
        
        [self.tableview reloadData];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        for (int i = 0; i< self.dataArray.count; i++) {
            indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            cell = [self.tableview cellForRowAtIndexPath:indexPath];
            cell.tx_Label.enabled = YES;
        }
    }
    self.EditOrOff = YES;
}

-(void)setupUI{
    
    self.tableview = [[UITableView alloc]init];
    self.tableview.delegate = self;
    self.tableview.dataSource =self;
    [self.view addSubview:self.tableview];
    
    //注册
    [self.tableview registerClass:[DiseaserTopView class] forHeaderFooterViewReuseIdentifier:@"head"];
    [self.tableview registerClass:[DesCell class] forCellReuseIdentifier:@"desCell"];
    
    self.tableview.tableFooterView = [[UIView alloc]init];
    
    self.topView.backgroundColor = [UIColor redColor];
    
}





/*****************左侧缩进为0*********************/
- (void)viewDidLayoutSubviews
{
    
    [super viewDidLayoutSubviews];
//    
//    [NSThread sleepForTimeInterval:3];
//    NSLog(@"%@",self.picImageView);
    
    if ([self.tableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableview setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    
    if ([self.tableview respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableview setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
/*****************左侧缩进为0*********************/


#pragma 取消选中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DesCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    [cell.tx_Label becomeFirstResponder];
    [self.tableview deselectRowAtIndexPath:indexPath animated:YES];
}



#pragma 约束设置
-(void)setupLayout{
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    
    

    
}

#pragma 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"desCell" forIndexPath:indexPath];
    DesModel*model = self.dataArray[indexPath.row];
    self.cell.model = model;
    
    self.cell.tx_Label.enabled= NO;
    return self.cell;
    
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    self.topView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    
    return self.topView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 100;
}

@end
